// pages/comment/comment.js
var app = getApp();
var pic_urls;
var inputlab;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goods = JSON.parse(options.goods);
    console.log(goods);
    pic_urls = [];
    inputlab = '';
    this.setData({
      goodsinfo: goods,
      cmm1: 0,
      cmm2: 0,
      cmm3: 0,
      pics: [],
      checked: []
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  cmm1StarTap: function (event) {
    var star = event.currentTarget.dataset.star;
    this.setData({
      cmm1: parseInt(star)
    });
  },
  cmm2StarTap: function (event) {
    var star = event.currentTarget.dataset.star;
    this.setData({
      cmm2: parseInt(star)
    });
  },
  cmm3StarTap: function (event) {
    var star = event.currentTarget.dataset.star;
    this.setData({
      cmm3: parseInt(star)
    });
  },
  chooseImage: function () {
    var that = this;
    wx.chooseImage({
      count: 6,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var pics = [];
        if (that.data['pics']) {
          pics = that.data['pics'];
        }
        if (pics.length > 6) {
          return;
        }
        var tempFilePaths = res.tempFilePaths;
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (pics.length >= 6) {
            break;
          }
          var picinfo = {};
          picinfo.note = "添加说明";
          picinfo.url = tempFilePaths[i];
          pics.push(picinfo);
        }
        that.setData({
          pics: pics
        });
      }
    })
  },
  picDelete: function (event) {
    var picindex = parseInt(event.currentTarget.dataset.picindex);
    var pics = this.data['pics'];
    pics.splice(picindex, 1);
    this.setData({
      pics: pics
    });
  },
  checkboxChange: function (event) {
    this.setData({
      checked: event.detail.value
    });
  },
  submitComment: function () {
    if (this.data.cmm1.toString() == '0' || this.data.cmm2.toString() == '0' || this.data.cmm3.toString() == '0') {
      wx.showToast({
        image: '../imgs/tips.png',
        title: '评论信息不完整',
      });
      return;
    }
    app.showLoading();
    this.uploadPic(0);
  },
  uploadPic: function (pindex) {
    var pics = this.data.pics;
    // console.log(pindex, pics.length);
    if (pindex >= pics.length) {
      this.submitInfo();
      return;
    }
    var that = this;
    var url = app.globalData.webUrl + '/index.php/api/Order/comment_upload/';
    wx.uploadFile({
      url: url,
      filePath: pics[pindex].url,
      name: 'file',
      success: function (res) {
        res = JSON.parse(res.data);
        // console.log(res);
        if (res.status == 'ok') {
          pic_urls.push(res.result);
        }
        that.uploadPic(pindex + 1);
      },
      fail: function (res) {
        // console.log(res);
      }
    })
  },
  submitInfo: function () {
    var imgs = pic_urls.join(',');
    // console.log(imgs);
    var hide_username = 0;
    if (this.data.checked.length > 0) {
      hide_username = 1;
    }
    var params = {
      goods_id: this.data.goodsinfo.goodsid,
      hide_username: hide_username,
      order_id: this.data.goodsinfo.orderid,
      service_rank: this.data.cmm2,
      deliver_rank: this.data.cmm3,
      goods_rank: this.data.cmm1,
      content: inputlab,
      imgs: imgs
    }
    var url = '/index.php/api/Order/add_comment';
    app.request(url,params).then(this.addCommentCallBack.bind(this));
  },
  addCommentCallBack: function (res) {
    // console.log(res);
    app.hideLoading();
    wx.showToast({
      icon: 'none',
      title: res.msg,
      duration: 2000
    });
    var delayid = setTimeout(function () {
      clearTimeout(delayid);
      wx.navigateBack({});
    }, 2000);
  },
  textInputCheck: function (event) {
    inputlab = event.detail.value;
  }
})